/*
 * PopupFileTransferExt.cpp
 *
 *  Created on: May 26, 2012
 *      Author: guillou
 */

#include "PopupUtilities.hpp"
#include "PopupFileTransferExt.hpp"

using namespace Popup;
using namespace std;

FileTransferExt::FileTransferExt(MessageID p_messageID,
                                 AttachmentID p_attachmentID,
                                 const UserList & p_targets,
                                 PopupUtils::FileStream * p_file,
                                 const string & p_filepath)
 : FileTransfer(POPUP_DIRECTION_OUT, p_messageID, p_attachmentID, p_filepath,
                p_file, PopupUtils::getFileSize(p_filepath)),
   totalNbFrames(0), frameNo(1), targets(p_targets), notificationPeriod(1),
   checksum(PopupUtils::checksum(p_filepath))
{
  // Make sureGet file size and compute the number of frames required to send
  // the attachment
  totalNbFrames = (totalSize / POPUP_MSG_SPLIT_SIZE);
  if (totalSize % POPUP_MSG_SPLIT_SIZE != 0) {
    totalNbFrames += 1;
  }

  // Compute nofification period
  computeNotificationPeriod();
}

FileTransferExt::FileTransferExt(MessageID p_messageID,
                                 AttachmentID p_attachmentID,
                                 PopupUtils::FileStream *p_file,
                                 const string & p_filepath,
                                 unsigned int p_nbFrames,
                                 unsigned int p_totalSize)
: FileTransfer(POPUP_DIRECTION_IN, p_messageID, p_attachmentID,
               p_filepath, p_file, p_totalSize),
  totalNbFrames(p_nbFrames), frameNo(1), notificationPeriod(1), checksum(0)
{
  // Compute nofification period
  computeNotificationPeriod();
}

void FileTransferExt::computeNotificationPeriod() {
  notificationPeriod = POPUP_MACRO_MAX(1, (totalNbFrames / 100));
}

bool FileTransferExt::isNotificationRequired() const {
  return ((frameNo % notificationPeriod) == 0 ||
          (frameNo == totalNbFrames));
}

bool FileTransferExt::isComplete() const {
  return (nbTransferred == totalSize);
}
